import tkinter as tk
from tkinter import messagebox
import re

win = tk.Tk()
# 设置主窗口
win.geometry('250x200+250+200')
win.title("C语言中文网")
win.iconbitmap('qianduan\logo.ico')
win.resizable(0,0)

# 创建验证函数

# 账号验证函数
def check_account():
    account = entry1.get()
    # 检查是否为纯数字
    if account.isdigit():
        return True
    else:
        messagebox.showwarning("验证失败", "账号必须为纯数字！")
        entry1.delete(0, tk.END)
        return False

# 密码验证函数
def check_password():
    password = entry2.get()
    # 检查长度是否大于8
    if len(password) <= 8:
        messagebox.showwarning("验证失败", "密码长度必须大于8！")
        entry2.delete(0, tk.END)
        return False
    
    # 检查是否包含大写字母
    if not re.search(r'[A-Z]', password):
        messagebox.showwarning("验证失败", "密码必须包含大写字母！")
        entry2.delete(0, tk.END)
        return False
    
    # 检查是否包含小写字母
    if not re.search(r'[a-z]', password):
        messagebox.showwarning("验证失败", "密码必须包含小写字母！")
        entry2.delete(0, tk.END)
        return False
    
    # 检查是否包含特殊字符（非字母数字）
    if not re.search(r'[^a-zA-Z0-9]', password):
        messagebox.showwarning("验证失败", "密码必须包含特殊字符！")
        entry2.delete(0, tk.END)
        return False
    
    else :
      messagebox.showinfo('',"登陆成功")
      entry1.delete(0, tk.END)
      entry2.delete(0, tk.END)
      return True
    

# 新建文本标签
labe1 = tk.Label(win,text="账号：")
labe2 = tk.Label(win,text="密码：")
labe1.grid(row=0)
labe2.grid(row=1)
# 创建动字符串
Dy_String = tk.StringVar()
# 使用验证参数 validata,参数值为 focusout 当失去焦点的时候，验证输入框内容是否正确
entry1 = tk.Entry(win,textvariable =Dy_String,validate ="focusout",validatecommand=check_account)
entry2 = tk.Entry(win, show="*", validate="focusout", validatecommand=check_password)

# 对控件进行布局管理，放在文本标签的后面
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)

win.mainloop()